package com.googlecode.recycled.camel;

import com.googlecode.recycled.lang.assertions.Assert;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;

/**
 * <p>Camel processor delegating transformation of the message to the external business component.</p>
 * <p>Immutable class.</p>
 */
public class BusinessTransformer implements Processor {

    /**
     * Business transformation delegate. Actual transformation is performed by
     * this instance. Cannot be null.
     */
    protected final BusinessTransformation businessTransformation;

    /**
     * Creates new BusinessTransformer.
     *
     * @param businessTransformation Business transformation delegate. Actual transformation
     *                               is performed by this instance. Cannot be null.
     */
    public BusinessTransformer(BusinessTransformation businessTransformation) {
        this.businessTransformation = Assert.notNull(businessTransformation);
    }

    @Override
    public void process(Exchange exchange) throws Exception {
        Object transformedMessage = businessTransformation.transform(exchange.getIn().getBody());
        if (transformedMessage == null) {
            exchange.setProperty(Exchange.ROUTE_STOP, true);
        } else {
            exchange.getIn().setBody(transformedMessage);
        }
    }

}
